home *** CD-ROM | disk | FTP | other *** search
- /*
- File: MoreAppleEvents.cp
-
- Contains:
-
- Written by: Pete Gontier
-
- Copyright: Copyright (c) 1998 Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
-
- <4> 2/15/99 PCG add AEGetDescDataSize for non-Carbon clients
- <3> 1/29/99 PCG add AEGetDescData
- <2> 11/11/98 PCG fix header
- <1> 11/10/98 PCG first big re-org at behest of Quinn
-
- Old Change History (most recent first):
-
- <2> 10/11/98 Quinn Convert "MorePrefix.h" to "MoreSetup.h".
- <2> 6/16/98 PCG CreateProcessTarget works with nil PSN
- <1> 6/16/98 PCG initial checkin
- */
-
- #include "MoreSetup.h"
- #include "MoreAppleEvents.h"
- #include "MoreProcesses.h"
-
- pascal OSErr SetReplyErrorNumber (OSErr errToReport, AppleEvent *reply)
- {
- OSErr err = noErr;
-
- if (reply->dataHandle)
- {
- if (!MoreAssert (reply->descriptorType == typeAppleEvent))
- err = paramErr;
- else
- err = AEPutParamPtr (reply,keyErrorNumber,typeShortInteger,&errToReport,sizeof(errToReport));
- }
-
- return err;
- }
-
- pascal OSErr SenderIsFinder (const AppleEvent *event, Boolean *isFinder)
- {
- if (!MoreAssert (event && isFinder)) return paramErr;
- if (!MoreAssert (event->descriptorType == typeAppleEvent)) return paramErr;
- if (!MoreAssert (event->dataHandle)) return paramErr;
-
- OSErr err = noErr;
- DescType actualType;
- ProcessSerialNumber senderPSN;
- Size actualSize;
-
- err = AEGetAttributePtr (event, keyAddressAttr, typeProcessSerialNumber, &actualType,
- (Ptr) &senderPSN, sizeof (senderPSN), &actualSize);
- if (MoreAssert (err == noErr))
- {
- if (!MoreAssert (actualType == typeProcessSerialNumber))
- err = paramErr;
- else if (!MoreAssert (actualSize == sizeof (senderPSN)))
- err = paramErr;
- else
- {
- ProcessInfoRec processInfo;
-
- if (!(err = GetSomeProcessInfo (&senderPSN,&processInfo)))
- {
- *isFinder = (processInfo.processSignature == 'MACS' && processInfo.processType == 'FNDR');
- }
- }
- }
-
- return err;
- }
-
- pascal OSErr CreateProcessTarget (ProcessSerialNumber *psn, AEDesc *target)
- {
- ProcessSerialNumber self;
-
- if (!psn)
- {
- psn = &self;
-
- self.lowLongOfPSN = kNoProcess;
- self.highLongOfPSN = kCurrentProcess;
- }
-
- return AECreateDesc (typeProcessSerialNumber,psn,sizeof(*psn),target);
- }
-
- pascal OSErr CreateSimpleAppleEvent
- (AEEventClass eventClass, AEEventID eventID, ProcessSerialNumber *psn, AppleEvent *result)
- {
- OSErr err = noErr;
-
- AEDesc target;
-
- if (!(err = CreateProcessTarget (psn,&target)))
- {
- OSErr err2;
-
- err = AECreateAppleEvent (eventClass,eventID,&target,kAutoGenerateReturnID,kAnyTransactionID,result);
-
- err2 = AEDisposeDesc (&target);
- if (!err) err = err2;
- }
-
- return err;
- }
-
- pascal OSErr SimplySendAppleEvent (const AppleEvent *appleEvent, AppleEvent *reply)
- {
- OSErr err = noErr;
-
- AESendMode aeSendMode = kAEAlwaysInteract | kAECanSwitchLayer;
-
- if (reply)
- {
- aeSendMode |= kAEWaitReply;
-
- reply->descriptorType = typeNull;
- reply->dataHandle = nil;
- }
-
- err = AESend (appleEvent, reply, aeSendMode, kAENormalPriority, kAEDefaultTimeout, nil, nil);
-
- return err;
- }
-
- #if !TARGET_CARBON
-
- pascal Size AEGetDescDataSize (const AEDesc *desc)
- {
- Size result = 0;
-
- if (MoreAssert (desc) && desc->dataHandle)
- {
- result = GetHandleSize (desc->dataHandle);
-
- if (!MoreAssert (noErr == MemError ( )))
- result = 0;
- }
-
- return result;
- }
-
- pascal OSErr AEGetDescData (const AEDesc *descP, DescType dType, void *dataP, Size max)
- {
- OSErr err = noErr;
-
- if (!MoreAssert (descP && (dataP || !max)))
- err = paramErr;
- else if (!MoreAssert (descP->descriptorType == dType))
- err = paramErr;
- else if (dataP && max)
- {
- Size actualSize = ::GetHandleSize (descP->dataHandle);
- if (!(err = MemError ( )))
- {
- if (actualSize > max) actualSize = max;
- ::BlockMoveData (*(descP->dataHandle),dataP,actualSize);
- }
- }
-
- return err;
- }
-
- #endif
-